package org.gdpu.action;

import com.opensymphony.xwork2.ModelDriven;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.gdpu.model.BloodOrder;
import org.gdpu.service.BloodOrderService;
import org.gdpu.vo.OfficeVO;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-11-19
 * Time: 下午9:20
 * To change this template use File | Settings | File Templates.
 */
@Component("office")
@Scope("prototype")
public class OfficeAction extends MyActionSupport implements ModelDriven<OfficeVO> {
    private final static Logger logger = LogManager.getLogger(OfficeAction.class);
    private final OfficeVO officeVO = new OfficeVO();
    private BloodOrderService bloodOrderService;


    public String findBloodByOffice()  {
        List<BloodOrder> bos = bloodOrderService.findBloodByOffice(officeVO.getOfficeName());
        if (officeVO.getOfficeName().isEmpty() || bos.size() == 0){
            officeVO.setResultMessage("该科室没有用血情况。");
            return ERROR;
        }
        double sum = 0.0d;
        for(int i = 0; i<bos.size(); i++){
            sum+=bos.get(i).getBloodVolume();
        }
        DecimalFormat df = new DecimalFormat("0.00");
        officeVO.setSum(sum);
        officeVO.getBos().addAll(bos);
        logger.info("find bloodOrder by office:" + officeVO.getOfficeName());
        view = "/page/office/findBloodByOffice.jsp";
        return VIEW;
    }

    @Resource(name = "bloodOrderServiceImpl")
    public void setBloodOrderService(BloodOrderService bloodOrderService) {
        this.bloodOrderService = bloodOrderService;
    }

    @Override
    public OfficeVO getModel() {
        return officeVO;
    }
}
